package io.finer.erp.jeecg.bas.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import io.finer.erp.jeecg.bas.dto.BasMaterialCategoryDTO;
import io.finer.erp.jeecg.bas.entity.BasMaterialCategory;

/**
 * @Description: 物料分类
 * @Author: jeecg-boot
 * @Date:   2020-05-29
 * @Version: V1.0
 */
public interface BasMaterialCategoryMapper extends BaseMapper<BasMaterialCategory> {

	/**
	 * 编辑节点状态
	 * @param id
	 * @param status
	 */
	void updateTreeNodeStatus(@Param("id") String id,@Param("status") String status);

	@Select("select * from (SELECT @r as _id, (select @r := pid FROM bas_material_category where id = _id) as 2v2, @l := @l + 1 as lvl FROM (SELECT @r := #{id}, @l:=0) vars, bas_material_category c WHERE @r is not null) T1 LEFT JOIN bas_material_category T2 ON T1._id=T2.id WHERE t2.pid=0")
	List<BasMaterialCategory> getRootCategory(@Param("id")String id);

	@Select("select * FROM bas_material_category where id in (select id from (select t1.id, if(find_in_set(pid, @pids) > 0, @pids := concat(@pids, ',', id), 0) as ischild from (select id,pid from bas_material_category t where t.is_enabled = 1 order by pid,id) t1,(select @pids := #{rootId}) t2 ) t3 where ischild != 0)")
	List<BasMaterialCategory> getChildCategory(@Param("rootId")String rootId);
	
	/**
	 * @Description 查询该分类下的所有ischild为0或者空的子节点
	 * @param rootId
	 * @return
	 * Create at: 2020-10-08 11:57:05
	 * @author: duanyong
	 * Revision:
	 *    2020-10-08 11:57:05 - first revision by duanyong
	 *        
	 */
	@Select("select * from bas_material_category where id in (select id from (select t1.id, if(find_in_set(pid, @pids) > 0, @pids := concat(@pids, ',', id), 0) as ischild from (select id,pid from bas_material_category t where t.is_enabled = 1 order by pid,id) t1,(select @pids := #{rootId}) t2 ) t3 where ischild != 0) and (has_child=0 OR has_child is NULL)")
	List<BasMaterialCategory> getAllChildCategory(@Param("rootId")String rootId);
	
	/**
	 * @Description 查询该分类下的所有子节点
	 * @param rootId
	 * @return
	 * Create at: 2020-10-12 11:41:05
	 * @author: duanyong
	 * Revision:
	 *    2020-10-12 11:41:05 - first revision by duanyong
	 *        
	 */
	@Select("select * from bas_material_category where id in (select id from (select t1.id, if(find_in_set(pid, @pids) > 0, @pids := concat(@pids, ',', id), 0) as ischild from (select id,pid from bas_material_category t where t.is_enabled = 1 order by pid,id) t1,(select @pids := #{rootId}) t2 ) t3 where ischild != 0)")
	List<BasMaterialCategory> getAllChildCategoryHasChild(@Param("rootId")String rootId);
	
	/**
	 * 查询物料分类
	 * @param id
	 * @return
	 */
	List<BasMaterialCategoryDTO> getBasMaterialCategoryDTO(@Param("id") List<String> id);

}
